home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12373 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  41 lines

  1. Path: news.gate.net!news-adm
  2. From: Dale Hollon <hollon@gate.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Malloc extended memory
  5. Date: Sun, 31 Mar 1996 13:20:54 -0500
  6. Organization: CyberGate, Inc.
  7. Message-ID: <315ECD06.5BE0@gate.net>
  8. References: <NEWTNews.31774.828246330.Postmaster@pi-user.pi.net>
  9. NNTP-Posting-Host: tpafl2-29.gate.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14.  
  15. rjvdiest@pi-user.pi.net wrote:
  16. > I would like a simple C-program that explains to me how to allocate extended
  17. > memory! Thanks.This will allocate memory for a string inputed by the user.
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21.  
  22. main()
  23. {
  24.     char temp[80], *string;
  25.     
  26.     printf("\n\nEnter a string.\n\n");
  27.     gets(temp);
  28.     /* malloc will return NULL if unsuccessful */    
  29.     string=(char *)malloc(strlen(temp)+1);
  30.     if(string==NULL)
  31.     {
  32.         printf("\n\nCould not allocate memory.\n");
  33.         exit(1);
  34.     }
  35.     strcpy(string,temp);
  36.     printf("\n\n%s\n",string);
  37.  
  38.     return 0;
  39. }
  40.